// TestClass.cpp: implementation of the TestClass class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "TestClass.h"
#include "expression\AccidenceAnalyzer.h"
#include "expression\SyntaxAnalyzer.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CPPUNIT_TEST_SUITE_REGISTRATION( TestClass );

void RunTest()
{
	CPPUNIT_NS::MfcUi::TestRunner runner;
	runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest() );
	runner.run();
}

TestClass::TestClass()
{

}

TestClass::~TestClass()
{

}

void TestClass::Test()
{
}

void TestClass::TestAccidenceAnalyzer()
{
	AccidenceAnalyzer Analyzer;
	CString strTemp;
	strTemp = _T("1+1");
	Analyzer.SetStatement(strTemp);

	CString strSymbol;
	Analyzer.MoveFirstSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T("1"));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T("+"));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T("1"));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol.IsEmpty());
}

void TestClass::TestRegularExpression()
{
	AccidenceAnalyzer Analyzer;
	CString strTemp;
	strTemp = _T("- size ++ + ++ b -- + -- asdf > 1 <= 1 = 1 == 1 > 1 >= 1 != 1*1/1 += 1 -=");

	Analyzer.SetStatement(strTemp);

	CString strSymbol;
	Analyzer.MoveFirstSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T("-"));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T("size"));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T("++"));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T("+"));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T("++"));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T("b"));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T("--"));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T("+"));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T("--"));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T("asdf"));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T(">"));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T("1"));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T("<="));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T("1"));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T("="));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T("1"));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T("=="));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T("1"));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T(">"));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T("1"));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T(">="));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T("1"));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T("!="));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T("1"));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T("*"));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T("1"));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T("/"));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T("1"));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T("+="));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T("1"));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol == _T("-="));
	Analyzer.MoveNextSymbol();
	strSymbol = Analyzer.GetCurSymbol();
	CPPUNIT_ASSERT(strSymbol.IsEmpty());
}

void TestClass::TestIterpret()
{
	CString strTemp;
	strTemp = _T("1000+10+1-1*2-2/2+1-1*2-2/2+1-1*2-2/2+1-1*2-2/2+1-1*2-2/2+1-1*2-2/2");


	SyntaxAnalyzer SAnalyzer(strTemp, (StackInterface<class CString,class Variable *, class Variable*> *)0x00000001);
	
	for(int i = 0 ; i < 100; i++)
	{
		CPPUNIT_ASSERT(SAnalyzer.Interpret());
		Variable var = SAnalyzer.Execute();
		CPPUNIT_ASSERT((INT)var == 998);
	}
}

void TestClass::TestBracket()
{
	CString strTemp;
	strTemp = _T("(-10+((10-1)*((5-5*(7-1))/(5))/9))/3 + 5");

	SyntaxAnalyzer SAnalyzer(strTemp, (StackInterface<class CString,class Variable *, class Variable* > *)0x00000001);
	
	CPPUNIT_ASSERT(SAnalyzer.Interpret());
	Variable var = SAnalyzer.Execute();
	CPPUNIT_ASSERT((INT)var == 0);
	
}